package com.serene.im.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.serene.im.enums.HttpCode;
import com.serene.im.utils.JsonResult;
import com.serene.im.pojo.SysUser;
import com.serene.im.pojo.vo.ImChatUserVo;
import com.serene.im.service.SysUserService;
import com.serene.im.utils.NetworkUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;


/**
 * 即时通讯聊天
 *
 * @author serence
 * @date 2021/3/18 15:55
 */

@RestController
@RequestMapping("/imChat/")
public class ImChatController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 显示聊天内容  退出聊天操作
     *
     * @param request
     * @return
     */
    @RequestMapping("showImChatInfo")
    public ModelAndView showImChatInfo(HttpServletRequest request) {
        System.out.println("进入显示聊天内容");
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/im_chat_info");
        return mv;
    }

    /**
     * 获得登录用户信息
     *
     * @param request
     * @return
     */
    @RequestMapping("login")
    public JsonResult<ImChatUserVo> login(HttpServletRequest request) {
        System.out.println("进入登录。。。登录成功后建立ws连接");
        HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
        String phone = request.getParameter("phone");
        if (StringUtils.isBlank(phone)) {
            httpCode.setMsg("手机号不为空");
            return new JsonResult<ImChatUserVo>(null, httpCode);
        }
        String password = request.getParameter("password");
        if (StringUtils.isBlank(password)) {
            httpCode.setMsg("密码不为空");
            return new JsonResult<ImChatUserVo>(null, httpCode);
        }
        QueryWrapper<SysUser> userWrapper = new QueryWrapper<SysUser>();
        userWrapper.eq("phone", phone);
        //查询用户信息
        SysUser sysUser = sysUserService.getOne(userWrapper);
        if (sysUser == null) {
            httpCode.setMsg("该用户信息不存在");
            return new JsonResult<ImChatUserVo>(null, httpCode);
        }
        String password2 = sysUser.getPassword();
        if (!password.equals(password2)) {
            httpCode.setMsg("账号或密码错误");
            return new JsonResult<ImChatUserVo>(null, httpCode);
        }
        ImChatUserVo chatUserVo = new ImChatUserVo();
        chatUserVo.setUser(sysUser);
        String ipAddress = NetworkUtil.getIpAddress(request);
        chatUserVo.setLoginIpLocation(ipAddress);
        return new JsonResult<ImChatUserVo>(chatUserVo, HttpCode.SUCCESS);
    }

    /**
     * 发送消息
     *
     * @param request
     * @return
     */
    @RequestMapping("sendMsg")
    public JsonResult<Object> sendMsg(HttpServletRequest request) {
        System.out.println("进入发送消息......");
        return new JsonResult<Object>(null, HttpCode.SUCCESS);
    }


}
